package com.extension.company.common.mvc;

import android.annotation.SuppressLint;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.extension.company.admin.R;
import com.extension.company.common.utils.DisplayUtils;
import com.extension.company.common.utils.Utils;
import com.jaeger.library.StatusBarUtil;

import butterknife.ButterKnife;

/**
 * 功能：带标题的activity基类
 */
public abstract class BaseTitleActivity extends AppCompatActivity {

	private LayoutInflater inflater;
	private LinearLayout m_top;
	private LinearLayout view_title; //标题布局
	private LinearLayout ll_view_main; // 内容部分位置
	private View view_main; // 内容部分
	public LinearLayout ll_loading_error; //加载失败视图
	public LinearLayout ll_loading;       //加载成功视图
	private LinearLayout ll_nodata;        //无数据视图
	private TextView tv_retry;             //重新加载按钮
	private TextView header_title; // 标题
	private ImageView m_back;   // 返回
	public TextView m_right; // 右键1
	private OnClickListener mOnClick;

	@SuppressLint("InlinedApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置键盘默认隐藏
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		setContentView(R.layout.base_activity);

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			//sdk大于或等于11
			//启动硬件加速
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
					WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
		}
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
			this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE
					| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
		} else {
			StatusBarUtil.setColor(this, getResources().getColor(R.color.black2));
		}
		initBaseView();
	}

	private void initBaseView() {
		m_top = getViewById(R.id.m_top);
		//如果4.4及以上可以让状态栏透明
//      	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//      		LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) m_top.getLayoutParams();
//      		layoutParams.height = Utils.getStatusBarHeight(this);
//      		m_top.setLayoutParams(layoutParams);
//      	}
		inflater = getLayoutInflater();
		view_title = getViewById(R.id.view_title);
		ll_view_main = getViewById(R.id.ll_view_main);
		ll_loading_error = getViewById(R.id.ll_loading_error);
		ll_loading = getViewById(R.id.ll_loading);
		ll_nodata = getViewById(R.id.ll_nodata);
		tv_retry = getViewById(R.id.tv_retry);

		// 加载内容视图
		view_main = inflater.inflate(getMainLayout(), null);

		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		//添加内容视图
		ll_view_main.addView(view_main, params);

		//添加标题视图
		view_title = findViewById(R.id.view_title);
		LinearLayout.LayoutParams params_title = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
				DisplayUtils.convertDip2Px(this, 44));
		view_title.addView(inflater.inflate(R.layout.base_header_title, null), params_title);

		// 返回
		m_back = getViewById(R.id.m_back);
		//右一按钮文字
		m_right = findViewById(R.id.m_right);
		// 标题
		header_title = getViewById(R.id.m_title);

		// 监听
		mOnClick = new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {

					case R.id.m_back:
						//返回
						back(v);
						break;
					case R.id.m_right:
						action_Right(v);
						break;
					case R.id.tv_retry:
						//重新加载
						action_ReTryData();
						updateLoadingView();
						break;
					default:
						break;
				}
			}
		};

		m_back.setOnClickListener(mOnClick);
		m_right.setOnClickListener(mOnClick);
		tv_retry.setOnClickListener(mOnClick);

		//显示加载视图
		updateLoadingView();
		ButterKnife.bind(this);
		//子类要实现的抽象方法
		initView();
		initData();
		initListener();
	}

	/**
	 * 获得主界面layout资源
	 *
	 * @return
	 */
	public abstract int getMainLayout();

	/**
	 * 注册组件
	 *
	 * @param id
	 * @param <T>
	 * @return
	 */
	protected <T extends View> T getViewById(int id) {
		return (T) findViewById(id);
	}

	/**
	 * 注册组件，基于View
	 *
	 * @param view
	 * @param id
	 * @param <T>
	 * @return
	 */
	protected <T extends View> T getViewById(View view, int id) {
		return (T) view.findViewById(id);
	}

	/**
	 * 取消方法�?
	 */
	public void onBtnCancel() {
		finish();
	}

	/**
	 * 标题栏左上角按钮事件
	 *
	 * @param v
	 */
	public void back(View v) {
		onBtnCancel();
	}

	/**
	 * 设置是否显示右键1
	 *
	 * @param show
	 */
	public void setShowRight1(boolean show) {
		if (show) {
			m_right.setVisibility(View.VISIBLE);
		} else {
			m_right.setVisibility(View.GONE);
		}
	}

	/**
	 * 标题栏右�?1文本
	 *
	 * @param str
	 */
	public void setTvRight1(String str) {
		m_right.setText(str);
	}

	/**
	 * 标题栏右�?1背景�?
	 *
	 * @param res
	 */
	public void setResourceRight1(int res) {
		m_right.setWidth(Utils.Dp2Px(this, 20));
		m_right.setHeight(Utils.Dp2Px(this, 20));
		m_right.setText("");
		m_right.setBackgroundResource(res);
	}

	/**
	 * 标题栏右键1背景图大小
	 */
	public void setResourceRight1Size(int with, int height) {
		LayoutParams params = new LayoutParams(Utils.Dp2Px(this, with), Utils.Dp2Px(this, height));
		//居右
		params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		//垂直居中
		params.addRule(RelativeLayout.CENTER_VERTICAL);
		params.rightMargin = Utils.Dp2Px(this, 10);
		m_right.setLayoutParams(params);
	}

	/**
	 * 获取m_right1
	 */
	public TextView getM_right() {
		return m_right;
	}

	/**
	 * 标题栏右�?1按钮事件
	 *
	 * @param v
	 */
	public void action_Right(View v) {
	}

	/**
	 * 设置标题
	 *
	 * @param title_str 标题内容
	 */
	public void setTitle(String title_str) {
		header_title.setText(title_str);
	}

	/**
	 * 获取标题布局
	 */
	public View getTitleView() {
		return view_title;
	}

	/**
	 * 子类控件初始化
	 */
	public abstract void initView();

	/**
	 * 子类数据初始化
	 */
	public abstract void initData();

	/**
	 * 设置子类监听器
	 */
	public abstract void initListener();

	/**
	 * 正在加载视图
	 */
	public void updateLoadingView() {
		ll_loading.setVisibility(View.VISIBLE);
		ll_loading_error.setVisibility(View.GONE);
		ll_view_main.setVisibility(View.GONE);
		ll_nodata.setVisibility(View.GONE);
	}

	/**
	 * 加载视图成功
	 */
	public void updateSuccessView() {
		ll_loading.setVisibility(View.GONE);
		ll_loading_error.setVisibility(View.GONE);
		ll_view_main.setVisibility(View.VISIBLE);
		ll_nodata.setVisibility(View.GONE);
	}

	/**
	 * 加载视图失败
	 */
	public void updateErrorView() {
		ll_loading.setVisibility(View.GONE);
		ll_loading_error.setVisibility(View.VISIBLE);
		ll_view_main.setVisibility(View.GONE);
		ll_nodata.setVisibility(View.GONE);
	}

	/**
	 * 无数据
	 */
	public void updateNodataView() {
		ll_loading.setVisibility(View.GONE);
		ll_loading_error.setVisibility(View.GONE);
		ll_view_main.setVisibility(View.GONE);
		ll_nodata.setVisibility(View.VISIBLE);
	}

	/**
	 * 重新加载数据
	 */
	public void action_ReTryData() {
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		if (newConfig.fontScale != 1) {
			//非默认值
			getResources();
		}
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public Resources getResources() {
		Resources res = super.getResources();
		if (res.getConfiguration().fontScale != 1) {//非默认值
			Configuration newConfig = new Configuration();
			newConfig.setToDefaults();//设置默认
			res.updateConfiguration(newConfig, res.getDisplayMetrics());
		}
		return res;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			onBtnCancel();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}



